package com.ysd.lis.service.lab.impl;

import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabDiagnosTemp;
import com.ysd.lis.entity.lab.LabDiagnosTempDesc;
import com.ysd.lis.entity.lab.LabDiagnosType;
import com.ysd.lis.mapper.lab.LabDiagnosTempDescMapper;
import com.ysd.lis.mapper.lab.LabDiagnosTempMapper;
import com.ysd.lis.mapper.lab.LabDiagnosTypeMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.request.lab.DiagnosTempDto;
import com.ysd.lis.service.lab.LabDiagnosTypeService;
import com.ysd.lis.util.ListToTreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 通道号管理表 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-10-29
 */
@Service
public class LabDiagnosTypeServiceImpl extends ServiceImpl<LabDiagnosTypeMapper, LabDiagnosType> implements LabDiagnosTypeService {

    @Autowired
    LabDiagnosTypeMapper labDiagnosTypeMapper;
    @Autowired
    LabDiagnosTempMapper labDiagnosTempMapper;

    @Autowired
    LabDiagnosTempDescMapper labDiagnosTempDescMapper;
    @Override
    public Result findDiagnosTypeTree(CommonPageDto pageParam) {
        LambdaQueryWrapper<LabDiagnosType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LabDiagnosType::getDelFlag,0);
        queryWrapper.orderByAsc(LabDiagnosType::getSeq);
        List<LabDiagnosType> dictTypeTree = labDiagnosTypeMapper.selectList(queryWrapper);
        JSONArray objects = ListToTreeUtil.forObjectToTreeMap(dictTypeTree, 0l, "id", "pid", "children");
        return Result.succ(1,"查询成功",objects);
    }

    @Override
    public Result addDiagnosType(LabDiagnosType sysBasDictType) {
        int insert = labDiagnosTypeMapper.insert(sysBasDictType);
        if(insert>0){
            return Result.succ(1, "添加成功", sysBasDictType);
        }else {
            return Result.succ(400, "添加失败", "");
        }
    }

    @Override
    public Result editDiagnosType(LabDiagnosType sysBasDictType) {
        int updateById = labDiagnosTypeMapper.updateById(sysBasDictType);
        if(updateById>0){
            return Result.succ(1, "修改成功", sysBasDictType);
        }else {
            return Result.succ(400, "修改失败", "");
        }
    }

    @Override
    public Result deletDiagnosType(LabDiagnosType sysBasDictType) {
        int deleteById = labDiagnosTypeMapper.deleteById(sysBasDictType.getId());
        if(deleteById>0){
            return Result.succ(1, "删除成功", sysBasDictType);
        }else {
            return Result.fail("删除失败");
        }
    }

    @Override
    public Result findDiagnosTypeAndTemp(DiagnosTempDto pageParam) {
        String code = pageParam.getCode();


        LambdaQueryWrapper<LabDiagnosType> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(LabDiagnosType::getDelFlag,0);
        queryWrapper1.eq(LabDiagnosType::getCode,code);

        List<LabDiagnosType> list = labDiagnosTypeMapper.selectList(queryWrapper1);
        LabDiagnosType labDiagnosType = list.get(0);


        LambdaQueryWrapper<LabDiagnosType> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(LabDiagnosType::getDelFlag,0);
        queryWrapper2.eq(LabDiagnosType::getPid,labDiagnosType.getId());
        queryWrapper2.orderByAsc(LabDiagnosType::getSeq);
        List<LabDiagnosType> dictTypeList = labDiagnosTypeMapper.selectList(queryWrapper2);
        //查出分类下的所有模板
        List<String> typeIds = dictTypeList.stream().map(LabDiagnosType::getId).collect(Collectors.toList());

        LambdaQueryWrapper<LabDiagnosTemp> queryWrapper3 = new LambdaQueryWrapper<>();
        queryWrapper3.eq(LabDiagnosTemp::getDelFlag,0);
        queryWrapper3.in(LabDiagnosTemp::getDiagnosTypeId,typeIds);
        queryWrapper3.orderByAsc(LabDiagnosTemp::getSeq);
        List<LabDiagnosTemp> tempList = labDiagnosTempMapper.selectList(queryWrapper3);


        //根据模板查询模板下的内容描述
        List<String> tempIds = tempList.stream().map(LabDiagnosTemp::getId).collect(Collectors.toList());
        LambdaQueryWrapper<LabDiagnosTempDesc> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LabDiagnosTempDesc::getDelFlag,0);
        queryWrapper.in(LabDiagnosTempDesc::getDiagnosTempId,tempIds);
        queryWrapper.orderByAsc(LabDiagnosTempDesc::getSeq);
        List<LabDiagnosTempDesc> descList = labDiagnosTempDescMapper.selectList(queryWrapper);

        for(LabDiagnosTemp temp : tempList){
            String id = temp.getId();
            List<LabDiagnosTempDesc> collect = descList.stream().filter(a -> a.getDiagnosTempId().equals(id)).collect(Collectors.toList());
            temp.setDiagnosTempDescList(collect);
        }


        for(LabDiagnosType type : dictTypeList){
            String id = type.getId();
            List<LabDiagnosTemp> collect = tempList.stream().filter(a -> a.getDiagnosTypeId().equals(id)).collect(Collectors.toList());
            type.setDiagnosTempList(collect);
        }

        return Result.succ(1,"查询成功",dictTypeList);
    }

    @Override
    public Result findParentDiagnosTypeList(DiagnosTempDto pageParam) {

        LambdaQueryWrapper<LabDiagnosType> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(LabDiagnosType::getDelFlag,0);
        queryWrapper1.eq(LabDiagnosType::getPid,"0");

        List<LabDiagnosType> list = labDiagnosTypeMapper.selectList(queryWrapper1);
        return Result.succ(1,"查询成功",list);
    }
}
